﻿using System;
using System.Linq;
using System.Windows.Input;
using CatalogModule.ViewModels;
using Microsoft.Practices.Prism.Regions;
using Infrastructure;
using CatalogModule.Views;
using System.ComponentModel;
using Microsoft.Practices.Prism.Events;
using Infrastructure.Events;
using System.Windows;
using Microsoft.Practices.Unity;
using System.Threading;
using System.Windows.Threading;
using Microsoft.Practices.Prism;

namespace CatalogModule.Commands
{
    public class ShowCauLacBoViewCommand : ICommand
    {
        #region Fields
        IUnityContainer container;
        IRegion contentRegion;
        BackgroundWorker worker;

        // Member variables
        private CatalogManagerMenuViewModel viewModel;
        Uri view;
        UriQuery query;
        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor.
        /// </summary>
        public ShowCauLacBoViewCommand(CatalogManagerMenuViewModel viewModel, IUnityContainer container)
        {
            this.viewModel = viewModel;
            this.container = container;

            container.RegisterType<Object, CauLacBoView>("CauLacBoView");
            contentRegion = viewModel.RegionManager.Regions[RegionNames.ContentRegion];
            query = new UriQuery();
            //query.Add("a","123");
            view = new Uri("CauLacBoView" + query.ToString(), UriKind.Relative);

            worker = new BackgroundWorker();
            worker.WorkerSupportsCancellation = true;
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
        }
        #endregion

        #region ICommand Members
        /// <summary>
        /// Whether the ShowModuleBViewCommand is enabled.
        /// </summary>
        public bool CanExecute(object parameter)
        {
            return true;
        }

        /// <summary>
        /// Actions to take when CanExecute() changes.
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        /// <summary>
        /// Executes the ShowModuleBViewCommand
        /// </summary>
        public void Execute(object parameter)
        {
            worker.RunWorkerAsync();
        }
        #endregion

        #region Private Methods
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            ////Type CurrentViewType = viewModel.RegionManager.Regions[RegionNames.ContentRegion].ActiveViews.FirstOrDefault().GetType();

            //Enable Busy Indicator
            viewModel.EventAggregator.GetEvent<IsBusyEvent>().Publish(true);

            Thread.Sleep(500);

            /* We invoke the NavigationCompleted() callback method in the next  
                 * navigation request since it is the last request we have to make. */
            Application.Current.Dispatcher.BeginInvoke(new ThreadStart(() => contentRegion.RequestNavigate(view)));
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var bw = sender as BackgroundWorker;
            if (!e.Cancelled)
            {
                //Disable Busy Indicator
                viewModel.EventAggregator.GetEvent<IsBusyEvent>().Publish(false);
            }
        }

        void PostNavigationCallback(NavigationResult navigationResult)
        {
        }
        #endregion
    }
}
